﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLKDS.DAL;
using DevExpress.XtraEditors.DXErrorProvider;

namespace QLKDS.Forms
{
    public partial class frmSupplierAdd : XtraForm
    {
        #region Properties
        
        private SupplierManager supplierManager;
      
        #endregion

        #region Methods

        private void SetMaxLengthOfTextBoxes()
        {
            this.txtName.Properties.MaxLength = 32;
            this.txtCode.Properties.MaxLength = 8;
            this.txtMobile.Properties.MaxLength = 16;
            this.txtPhone.Properties.MaxLength = 16;            
        }

        /// <summary>
        /// Clear all Textboxes on this form
        /// </summary>
        private void ClearTextBoxes()
        {
            this.txtCode.Text = String.Empty;
            this.txtName.Text = String.Empty;
            this.txtMobile.Text = String.Empty;
            this.txtPhone.Text = String.Empty;
            this.txtAddress.Text = String.Empty;
            this.txtEmail.Text = String.Empty;
            this.txtDescription.Text = String.Empty;
            this.dteCreatedDate.DateTime = DateTime.Now;
            this.dteCreatedDate.Properties.MaxValue = DateTime.Now;
            this.txtName.Focus();
        }
        #endregion

        #region Init form
        public frmSupplierAdd()
        {
            InitializeComponent();
            this.supplierManager = new SupplierManager();
          
        }

        private void frmStoreAdd_Load(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
            this.SetMaxLengthOfTextBoxes();
           
        }
        #endregion

        #region Validate

        private void txtName_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepName.ClearErrors();
            }
        }
        private void txtName_Leave(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                this.dxepName.SetError(this.txtName, "Tên khách hàng không được để trống.", ErrorType.User1);
            }
        }

        private void txtCode_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtCode.Text.Trim() == String.Empty)
            {
               //
            }
            else
            {
                if (this.supplierManager.HasCodeExisted(this.txtCode.Text.Trim()))
                {
                    this.dxepCode.SetError(this.txtCode, "Mã khách hàng " + this.txtCode.Text.Trim() + " đã tồn tại trong hệ thống. Hãy chọn một mã khách hàng khác.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                    this.txtCode.Focus();
                    return;
                }

                this.dxepCode.ClearErrors();
            }
        }
        private void txtCode_Leave(object sender, EventArgs e)
        {
            if (this.txtCode.Text.Trim() == String.Empty)
            {
                this.dxepCode.SetError(this.txtCode, "Mã khách hàng không được để trống.", ErrorType.User1);
            }
        }
        private void txtLiabilities_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtLiabilities.Text.Trim() == String.Empty)
            {
                this.dxepLiabilities.ClearErrors();
            }
        }



        private void txtMobile_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtMobile.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                if (this.supplierManager.HasMobileExisted(this.txtMobile.Text.Trim()))
                {
                    this.dxepMobile.SetError(this.txtMobile, "Số điện thoại khách hàng " + this.txtEmail.Text.Trim() + " đã tồn tại trong hệ thống. Vui lòng kiểm tra lại.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                    this.txtMobile.Focus();
                    return;
                }

                this.dxepMobile.ClearErrors();
            }
        }

        private void txtEmail_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtEmail.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                if (this.supplierManager.HasEmailExisted(this.txtEmail.Text.Trim()))
                {
                    this.dxepEmail.SetError(this.txtEmail, "Email khách hàng " + this.txtEmail.Text.Trim() + " đã tồn tại trong hệ thống. Vui lòng kiểm tra lại.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                    this.txtEmail.Focus();
                    return;
                }

                this.dxepEmail.ClearErrors();
            }
        }

        private void txtCreatedBy_Leave(object sender, EventArgs e)
        {
            if (this.txtCreatedBy.Text.Trim() == String.Empty)
            {
                this.dxepCreatedBy.SetError(this.txtCode, "Lựa chọn tên nhân viên thêm mới.", ErrorType.User1);
            }
        
        }

        private void txtCreatedBy_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtCreatedBy.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepCreatedBy.ClearErrors();
            }
        }

        #endregion

        #region Button_click
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            var supplier = new Supplier();


            //validation name
            if (this.txtName.Text.Trim().Length == 0)
            {
                this.dxepName.SetError(this.txtName, "Tên khách hàng không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtName.Focus();
                return;
            }
            //validation Code
            else if (this.txtCode.Text.Trim().Length == 0)
            {
                this.dxepCode.SetError(this.txtCode, "Mã khách hàng không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtCode.Focus();
                return;
            }
            else if (this.supplierManager.HasCodeExisted(this.txtCode.Text.Trim()) == true)
            {
                this.dxepName.SetError(this.txtCode, "Mã khách hàng " + this.txtCode.Text.Trim() + " đã tồn tại trong hệ thống. Hãy chọn một mã khách hàng khác.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtName.Focus();
                return;
            }
            //end validation Code
            else if (this.txtLiabilities.Text.Trim() == String.Empty)
            {
                this.dxepLiabilities.SetError(this.txtLiabilities, "Nhập công nợ ban đầu");
                this.txtLiabilities.Focus();
                return;
            }
            //Validation CreatedBy
            else if (this.txtCreatedBy.Text.Trim().Length == 0)
            {
                this.dxepCreatedBy.SetError(this.txtCreatedBy, "Lựa chọn tên nhân viên thêm mới");
                this.txtCreatedBy.Focus();
                return;
            }
            else
            {
                this.dxepLiabilities.ClearErrors();
                this.dxepMobile.ClearErrors();
                this.dxepName.ClearErrors();
                this.dxepEmail.ClearErrors();
                this.dxepCode.ClearErrors();
                this.dxepCreatedBy.ClearErrors();
            }
            //End validation CreatedBy

            // End of validation
            
            /*****************************************************************/
            // Save into database                   

            supplier.Code = this.txtCode.Text.Trim();
            supplier.Name = this.txtName.Text.Trim();
            supplier.Mobile = this.txtMobile.Text.Trim();
            supplier.Liabilities = float.Parse(this.txtLiabilities.Text.Trim());
            supplier.Phone = this.txtPhone.Text.Trim();
            supplier.Address = this.txtAddress.Text.Trim();
            supplier.Email = this.txtEmail.Text.Trim();
            supplier.Fax = this.txtFax.Text.Trim();
            supplier.Website = this.txtWebsite.Text.Trim();
            supplier.TaxCode = this.txtTaxCode.Text.Trim();
            supplier.Description = this.txtDescription.Text.Trim();

            supplier.Status = 1;

            supplier.CreatedBy = this.txtCreatedBy.Text;
            supplier.CreatedDate = this.dteCreatedDate.DateTime;
            supplier.ModifiedBy = "admin";
            supplier.ModifiedDate = this.dteCreatedDate.DateTime;
            supplier.Contactor = this.txtContactor.Text.Trim();
            supplier.ContactorEmail = this.txtContactorEmail.Text.Trim();
            supplier.ContactorMobile = this.txtContactorMobile.Text.Trim();
            supplier.ContactorPhone = this.txtContactorPhone.Text.Trim();

            this.supplierManager.CreateSupplier(supplier);

         

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        #endregion

       
      

 

      

     
        


    }
}
